package Nitrus;

import java.sql.*;

public class Agente {

	private Connection conn;
	private final static String DBCONN = "jdbc:mysql://mysql21.freehostia.com:3306/mxlcam_ydark";
	private final static String DRIVER = "com.mysql.jdbc.Driver";
	private final static String USER = "mxlcam_ydark";
	private final static String PASS = "vivayo";
	protected static Agente mInstancia = null;
	public Connection getConn() { return conn; }
	public void setConn(Connection conn) { this.conn = conn; }
	private Agente() throws Exception {
		try {
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(DBCONN,USER,PASS);
		} catch (ClassNotFoundException e) {
		      System.err.println("Excepcion:"); 
		      System.err.println(e.getMessage()); 
		} catch (SQLException e) {
		      System.err.println("Excepcion:"); 
		      System.err.println(e.getMessage()); 
		}
	}
	public static Agente getAgente() throws Exception {
		if (mInstancia == null) {
			mInstancia = new Agente();
		}
		return mInstancia;
	}
	public void create(String query) throws Exception {
		PreparedStatement st = conn.prepareStatement(query);			
		st.executeUpdate();
		conn.commit();
		st.close();
	}
	public ResultSet read(String query) throws Exception {
		PreparedStatement st = conn.prepareStatement(query);
		ResultSet resQuery = st.executeQuery();
		resQuery.close();
		st.close();					
		return resQuery;
	}
	public void update(String query) throws Exception {
		PreparedStatement st = conn.prepareStatement(query);
		st.execute();		
		st.close();
	}	
	public void delete(String query) throws Exception {
		PreparedStatement st = conn.prepareStatement(query);
		st.executeUpdate();
		st.close();
		conn.commit();
	}
}